//
//  DYZNetworkManager.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZNetworkManager.h"

@implementation DYZNetworkManager

+ (instancetype)sharedManager {
    static DYZNetworkManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DYZNetworkManager alloc]initWithBaseURL:nil];
        instance.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    });
    return instance;
}


/**
 *  获取网络数据的API
 *
 *  @param urlString  urlString
 *  @param parameters 参数
 *  @param completion 完成之后的回调
 */

- (void)loadDataWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id, NSError *))completion {
    
    [self GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completion(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(nil,error);
    }];
}

+ (void)getUrlString:(NSString *)url
           withParam:(NSDictionary *)param
    withSuccessBlock:(successBlock)success
     withFailedBlock:(failedBlock)failed
      withErrorBlock:(errorBlock)error{
    
    [[self sharedManager] GET:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
        
        
        success(responseObject);
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failed(error);
        
    }];
    
    
}

+ (void)postUrlString:(NSString *)url
            withParam:(NSDictionary *)param
     withSuccessBlock:(successBlock)success
      withFailedBlock:(failedBlock)failed
       withErrorBlock:(errorBlock)error{
    
    [[self sharedManager] POST:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failed(error);
        
    }];
    
}
@end
